from datetime import datetime

from rest_framework import serializers

from user.models import ProjUserGroup, ProjUser


class ProjUserGroupPostSerializer(serializers.ModelSerializer):
    members = serializers.PrimaryKeyRelatedField(queryset=ProjUser.objects.all(), many=True)

    class Meta:
        model = ProjUserGroup
        fields = ('name', 'description', 'members', 'create_time')  # 不要提交

    def create(self, validated_data):
        validated_data['create_time'] = datetime.now()
        return super().create(validated_data)


class ProjUserGroupPutSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProjUserGroup
        fields = ('name', 'description')  # 只允许更新这两个字段


